package com.online.service_edu.controller;


import com.online.common_utils.ResultJson;
import com.online.service_edu.entity.EduChapter;
import com.online.service_edu.entity.treeChapter.ChapterVO;
import com.online.service_edu.service.EduChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author hdc2008
 * @since 2021-02-11
 */
@RestController
@RequestMapping("/service_edu/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    private EduChapterService eduChapterService;

    @GetMapping("chapterList/{id}")
    public ResultJson getChapterVOlist(@PathVariable String id){
        List<ChapterVO> chapterVOList=eduChapterService.getChapterList(id);
        return ResultJson.ok().data("chapterVOList",chapterVOList);
    }

    @PostMapping("addChapter")
    public ResultJson addChapterInfo(@RequestBody EduChapter chapter){
        boolean flag = eduChapterService.save(chapter);
        if(flag){
            return ResultJson.ok().data("chapterId",chapter.getId());
        }else{
            return ResultJson.error().message("章节添加不成功");
        }
    }

    @GetMapping("getChapterById/{id}")
    public ResultJson getChapterById(@PathVariable String id){
        EduChapter eduChapter = eduChapterService.getById(id);
        return ResultJson.ok().data("chapter",eduChapter);
    }

    @PostMapping("updateChapter")
    public ResultJson updateChapter(@RequestBody EduChapter chapter){
        boolean flag = eduChapterService.updateById(chapter);
        if(flag){
            return ResultJson.ok();
        }else{
            return ResultJson.error().message("章节添加不成功");
        }
    }

    @GetMapping("delete/{id}")
    public ResultJson deleteChapterById(@PathVariable String id){
        boolean flag=eduChapterService.removeChapterById(id);
        if(flag){
            return ResultJson.ok();
        }else{
            return ResultJson.error().message("请删除完小节，才能删除章");
        }
    }
}

